/*
 * Copyright (c) 2009, Servoy-stuff
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 * http://www.fsf.org/licensing/licenses/lgpl.txt
 */

package net.stuff.servoy.beans.calendar.wicket;

import net.stuff.servoy.beans.calendar.IDateChooserBeanComponent;

import org.apache.wicket.validation.IValidatable;
import org.apache.wicket.validation.ValidationError;
import org.apache.wicket.validation.validator.AbstractValidator;

import com.servoy.j2db.util.Debug;

/**
 * 
 * @author servoy-stuff
 * http://www.servoy-stuff.net/
 */
public class DateStringValidator extends AbstractValidator {

	private static final long serialVersionUID = 1L;
	
	private IDateChooserBeanComponent parent;
	
	public DateStringValidator(IDateChooserBeanComponent parent) {
		super();
		this.parent = parent;
		
	}

	/* (non-Javadoc)
	 * @see org.apache.wicket.validation.validator.AbstractValidator#onValidate(org.apache.wicket.validation.IValidatable)
	 */
	@Override
	protected void onValidate(IValidatable obj) {
		if (parent.getValidationEnabled()) {
			Object value = obj.getValue();
			Debug.log(value);
			if (value != null && value instanceof String && ((String)value).trim().length() > 0) {
				ValidationError err = new ValidationError();
				err.setMessage("Date format not valid!");
				Debug.log(err.getMessage());
				obj.error(err);
			}
		}
	}
	
}
